<%@ page language="java" import="com.newcrm.config.*,com.newcrm.util.Util,java.net.URLEncoder,com.newcrm.bean.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	Cookie[] cookies = request.getCookies();
  String userId = Util.getCookieValue(cookies, Const.COOKIE_USERID);
  if ((userId == null) || (userId.length() == 0)) {
    userId = (String) request.getAttribute(Const.COOKIE_USERID);
  }
  if (userId == null) {
    userId = "";
  }

  String ip = request.getLocalAddr();
  int port = request.getLocalPort();
  String mobileUrl = "http://" + ip;
  if (port != 80) {
    mobileUrl = mobileUrl + ":" + port;
  }
  mobileUrl = mobileUrl + request.getContextPath() + "/m";
  // 获取产品信息
  ProductInfo productInfo = Config.getInstance().productInfo;
  String productName = productInfo.productName;
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title><%=productName%>客户管理系统</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta name="renderer" content="webkit"/>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  <link rel="shortcut icon" href="<%=request.getContextPath()%>/images/qikebao.ico" />
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/all.css?ver=<%=Const.VERSION%>">
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/font-awesome/css/font-awesome.min.css">
  <script type="text/javascript" src="script/jquery-1.11.1.min.js"></script>
  <script type="text/javascript" src="script/common.js?ver=<%=Const.VERSION%>"></script>
  <script>
    $(function() {
      $('#reload-captchaCode-link').click(function(e) {
        e.preventDefault();
        reloadCaptchaCode();
      });

      var _u = $("#username");
      var _p = $("#password");


      var _userId = "<%=userId%>";
      if (_userId != "") {
        _u.val(_userId);
        _p.focus();
      } else {
        _u.focus();
      }
      $("#qrcode-link").click(function(e) {
        e.preventDefault();
        $("#qrcode-content").show();
      });
      $("#qrcode-back-link").click(function(e) {
        e.preventDefault();
        $("#qrcode-content").hide();
      });
      $('#loginForm').submit(function(event){
        var _username = _u.val();
        var _password = _p.val();

        var _c = $("#captcha");
        var _captcha = _c.val();

        if (_username == "") {
          alert("请输入登录用户名");
          _u.focus();
          return false;
        }
        if (_password == "") {
          alert("请输入登录密码");
          _p.focus();
          return false;
        }
        if (_captcha == "") {
          alert("请输入验证码");
          _c.focus();
          return false;
        }
        var params = {};
        params.userId = _username;
        params.password = _password;
        params.captcha = _captcha;
        $("#button-panel").addClass("busy");
        $.ajax({
          url: '<%=request.getContextPath()%>/login?random=' + Math.random(),
          type: 'GET',
          data:params,
          dataType: 'json',
          timeout: 60000,
          error: function(){
          },
          success: function(json){
            $("#button-panel").removeClass("busy");
            if (json.error == "3") {
              reloadCaptchaCode();
              alert("产品验证码检查失败，请联系管理员！详细如下："+ json.msg);
              _u.focus();
              return;
            }

            if (json.error == "2") {
              reloadCaptchaCode();
              alert("请输入的验证码不正确，请确认后再输入");
              _c.focus();
              return;
            }
            if (json.error == "1") {
              reloadCaptchaCode();
              alert("您输入的用户名或密码错误，请确认后，重新输入");
              _u.focus();
              return;
            }

            var _url = QueryString.url;
            if (_url && _url.length > 0) {
              window.location = _url;
              return;
            }
            window.location = "<%=request.getContextPath()%>/dashboard";
          }
        });
        return false;
      });
    });

    function reloadCaptchaCode() {
      $("#captchaCodeImage").hide().attr('src','<%=request.getContextPath()%>/code.jpg?rand=' + Math.floor(Math.random() * 100)).fadeIn();
    }
  </script>
</head>
<body class="login">
  <img src="<%=request.getContextPath()%>/images/bg.png" class="bg">
  <div class="login-content">
    <div class="logo"><img src="<%=request.getContextPath()%>/images/logo<%=productInfo.logoImageExtInfo%>.png"></div>
    <div class="dialog">
      <img src="<%=request.getContextPath()%>/images/login.png" class="banner">
      <form method="post" id="loginForm">
        <div class="form">
          <div class="username"><input type="text" class="txt" id="username" value="" placeholder="请输入用户名"></div>
          <div class="password"><input type="password" class="txt" id="password" value="" placeholder="请输入密码"></div>
          <div class="password captcha">
            <input type="text" class="txt min" id="captcha" value="" placeholder="请输入验证码">
            <a href="#" id="reload-captchaCode-link">
              <img id="captchaCodeImage" src="<%=request.getContextPath()%>/code.jpg" style="width:100px;">
            </a>
          </div>
        </div>
        <div class="signin">
          <div class="right" id="button-panel"><input type="submit" class="button" value="登录"></div>
          <!--
          <div class="remember-panel">
            <a href="#" id="qrcode-link" title="手机端登陆" v="http://fir.im/qikebao"><i v="0" class="fa fa-2x fa-qrcode"></i> <span>手机登录</span></a>
          </div>
          -->
        </div>
      </form>
      <div class="qrcode" id="qrcode-content">
        <h1>扫描二维码，安装手机版<%=productName%></h1>
        <div id="qrcode-img"><img width="200" src="http://www.qikebao.com/images/qikebaoapp.png"></div>
        <p><a href="#" id="qrcode-back-link">返回</a></p>
      </div>
    </div>
  </div>
</body>
</html>